package com.crm.controller;

import com.crm.entity.SysUser;
import com.crm.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class ExampleController {
    @Resource
    private SysUserService sysUserService;

    @GetMapping("/hello/{id}")
    public String getUser(@PathVariable("id") long usrId, Model model){
        SysUser sysUser = sysUserService.getUser(usrId);
        model.addAttribute("user",sysUser);
        System.out.println(1);
        return "hello";
    }

    @GetMapping("/string")
    public String getString(Model model, HttpServletRequest request){
        //演示赋值、字符串拼接
        model.addAttribute("userName","ktjiaoyu");
        request.setAttribute("test","request");
        request.getSession().setAttribute("test","session");
        model.addAttribute("date",new Date());
        return "string";
    }

    @GetMapping("/inline")
    public String inline(Model model){
        model.addAttribute("userName","ktjiaoyu");
        return "inline";
    }

    @GetMapping("/if")
    public String ifunless(Model model){
        model.addAttribute("flag","yes");
        return "if";
    }

    @GetMapping("/switch")
    public String switchfor(Model model){
        model.addAttribute("sex","牛子");
        return "switch";
    }

    @GetMapping("/list")
    public String listfor(Model model){
        List<SysUser> list = sysUserService.getListUser();
        model.addAttribute("users",list);
        for (SysUser sysUser : list) {
            System.out.println(sysUser.toString());
        }
        return "list";
    }
}
